import { createRouter, createWebHashHistory } from "vue-router";
import { showToast } from "vant";
const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: "/",
      redirect: "/home",
    },
    {
      path: "/home",
      name: "home",
      component: () => import("@/views/home"),
    },
    {
      path: "/cart",
      name: "cart",
      meta: { isAuth: true },
      component: () => import("@/views/cart"),
    },
    {
      path: "/order",
      name: "order",
      meta: { isAuth: true },
      component: () => import("@/views/order"),
    },
    {
      path: "/mine",
      name: "mine",
      meta: { isAuth: true },
      component: () => import("@/views/mine"),
    },
    {
      path: "/store",
      name: "store",
      component: () => import("@/views/store"),
    },
    {
      path: "/createorder",
      name: "createorder",
      meta: { isAuth: true },
      component: () => import("@/views/createOrder"),
    },
    {
      path: "/address",
      name: "address",
      meta: { isAuth: true },
      component: () => import("@/views/address"),
    },
    {
      path: "/addressedit",
      name: "addressedit",
      meta: { isAuth: true },
      component: () => import("@/views/addressEdit"),
    },
    {
      path: "/login",
      name: "login",
      component: () => import("@/views/login"),
    },
    {
      path: "/register",
      name: "register",
      component: () => import("@/views/register"),
    },
    {
      path: "/userinfoedit",
      name: "userinfoedit",
      component: () => import("@/views/userinfoEdit"),
    },
    {
      path: "/:pathMatch(.*)", //pathMatch(.*) .匹配除换行之外的任何单个字符，*匹配前面的字符零次或多次
      component: () => import("@/views/notpage"),
    },
  ],
});

router.beforeEach((to, from, next) => {
  if (to.meta.isAuth) {
    // 判断一下localstorage中有没有登录标识
    if (localStorage.token) {
      next();
    } else {
      next("/login");
      showToast("请先登录");
    }
  } else {
    next();
  }
});

export default router;
